﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Jb.Common.Extensions;
using Jb.Domain;
using Jb.Web.ViewModels;
using AutoMapper;

namespace Jb.Web.Utility
{
    public static class SessionCache
    {
        public static UserViewModel CurrentUser
        {
            get
            {
                if (HttpContext.Current.Session == null)                
                    return null;
                
                if (HttpContext.Current.Session["CURRENT_USER"] == null) return null;
                return HttpContext.Current.Session["CURRENT_USER"] as UserViewModel;
            }
            set
            {
                if (HttpContext.Current.Session != null)                
                    HttpContext.Current.Session["CURRENT_USER"] = value;                
            }
        }

        public static String RedirectUrl
        {
            get
            {
                if (HttpContext.Current.Session == null)
                    return null;

                if (HttpContext.Current.Session["REDIRECT_URL"] == null) return null;
                return HttpContext.Current.Session["REDIRECT_URL"] as String;
            }
            set
            {
                if (HttpContext.Current.Session != null)
                    HttpContext.Current.Session["REDIRECT_URL"] = value;
            }
        }

        #region Public Methods

        /// <summary>
        /// Builds and Updates JobSeeker or Employer info in the SessionCache
        /// according to the type of Currently logged in user.
        /// </summary>
        public static void RefreshUserInfo()
        {
            if (SessionCache.CurrentUser != null)
            {
                JbEntities context = new JbEntities();
                if (UserInfo.IsCompanyUser)
                {
                    EmployerInfo empInfo = SessionCache.CurrentUser.EmployerInfo;
                    if (empInfo == null)
                        empInfo = new EmployerInfo();

                    var totalJobs = from P in context.Jobs
                                    where P.CompanyID == SessionCache.CurrentUser.Contact.CompanyID && P.IsDeleted == false
                                    select new
                                    {
                                        ID = P.ID,
                                        IsActive = P.IsActive,
                                        EndDate = P.EndDate
                                    };

                    empInfo.TotalJobs = totalJobs.Count();
                    //Set Published Jobs
                    empInfo.TotalActiveJobs = totalJobs.Count(P => P.IsActive && P.EndDate >= DateTime.Now);
                    empInfo.TotalUnPublishedJobs = totalJobs.Count(P => P.IsActive == false);
                    ///Load Package Subscription Info
                    PackageSubscription subscriptionEntity = PackageInfo.GetSubscribedPackage();
                    empInfo.Subscription = Mapper.Map<PackageSubscription, PackageSubscriptionViewModel>(subscriptionEntity);
                    //Refresh ContactViewModel
                    Contact contact = context.Contacts.FirstOrDefault(P => P.ID == SessionCache.CurrentUser.ContactID);
                    SessionCache.CurrentUser.Contact = Mapper.Map<Contact, ContactViewModel>(contact);
                    //Set EmpInfo
                    SessionCache.CurrentUser.EmployerInfo = empInfo;
                    ///Set the JobSeekerInfo to Null, it is unnecessary for an Employer
                    SessionCache.CurrentUser.JobSeekerInfo = null;
                }
                else if (UserInfo.IsJobSeeker)
                {
                    JobSeekerInfo jsInfo = SessionCache.CurrentUser.JobSeekerInfo;
                    if (jsInfo == null)
                        jsInfo = new JobSeekerInfo();

                    JobSeeker jobSeeker = context.GetJobSeekerByID(SessionCache.CurrentUser.JobSeekerID);

                    jsInfo.AppliedJobs = context.JobApplications.Count(P => P.Resume.JobSeekerID == SessionCache.CurrentUser.JobSeekerID);
                    jsInfo.FavoriteJobs = context.FavoriteJobs.Count(P => P.UserID == SessionCache.CurrentUser.ID);
                    jsInfo.HasProfileCompleted = jobSeeker.HasProfileCompleted; //SessionCache.CurrentUser.JobSeeker.HasProfileCompleted.ToBoolean();
                    jsInfo.TotalResumes = jobSeeker.Resumes.Count(); //context.Resumes.Count(P => P.JobSeekerID == SessionCache.CurrentUser.JobSeekerID);
                    SessionCache.CurrentUser.JobSeekerInfo = jsInfo;
                    ///Refreshing the JobSeekerViewModel
                    SessionCache.CurrentUser.JobSeeker = Mapper.Map<JobSeeker, JobSeekerViewModel>(jobSeeker);
                    ///Set the EmployerInfo to Null, it is uncenessary for a JobSeeker
                    SessionCache.CurrentUser.EmployerInfo = null;
                }
            }
        }

        /// <summary>
        /// Clears the session.
        /// </summary>
        public static void ClearSession()
        {
            HttpContext.Current.Session.Clear();
        }

        #endregion
    }
}